<template>
  <div>
    <van-nav-bar
      title="登录/注册"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <h3 class="login-title">请登录或注册</h3>
    <van-form @submit="onSubmit">
      <van-field
        v-model="loginForm.phone"
        name="手机"
        label="手机"
        placeholder="手机"
        :rules="[{ required: true, message: '请填写手机号' }]"
      />
      <van-field
        v-model="loginForm.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录/注册</van-button
        >
      </div>
    </van-form>
    <h3 class="login-title">其他方式登录</h3>
    <div class="btns">
      <van-button
        class="btn-item"
        round
        block
        type="primary"
        size="small"
        @click="wxLogin"
      >
        <img
          src="@/assets/img/wb.png"
          alt="wx"
          style="width: 30px; heigth: 30px"
        />
      </van-button>
      <van-button
        class="btn-item"
        round
        block
        type="danger"
        size="small"
        @click="wbLogin"
      >
        <img
          src="@/assets/img/wx.png"
          alt="wb"
          style="width: 30px; heigth: 30px"
        />
      </van-button>
    </div>
  </div>
</template>

<script>
import memberApi from "@/api/member/member";
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        phone: "",
        password: "",
      },
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go("-1");
    },
    onSubmit() {
      memberApi
        .logins(this.loginForm)
        .then((res) => {
          if(res.code ===20000){
            this.$toast("登录/注册成功")
            sessionStorage.setItem('userInfo',JSON.stringify(res.data.data))
            this.$router.push("/mine")
          }else{
            this.$toast("登录/注册失败，请重试")
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    wxLogin() {
      this.$toast("暂未开放");
    },
    wbLogin() {
      this.$toast("暂未开放");
    },
  },
};
</script>

<style lang="scss" scoped>
.login-title {
  line-height: 50px;
  text-align: center;
}
.btns {
  display: flex;
  justify-content: space-around;
  margin: 0 30px;
  .btn-item {
    width: 40%;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
}
</style>